<?php
require_once dirname(__FILE__) . "/../config/functions.php";
include_once dirname(__FILE__) . '/header.php';

if (isset($_POST) && $_POST) {
    //save-update confirmation_email
    $subject = $_POST['confirmation_email_subject'];
    $body = $_POST['confirmation_email_message'];
    $check = mysql_query("SELECT * FROM `mail_template` where `code` = 'confirmation_email' ");
    if (mysql_num_rows($check) > 0) {
        mysql_query("update `mail_template` set `subject` = '$subject',`body` = '$body' where `code` = 'confirmation_email' ");
    } else {
        mysql_query("insert into `mail_template` (`code`, `subject`, `body`) values ('confirmation_email' ,'$subject', '$body') ");
    }

    //save-update welcome_email
    $subject = $_POST['welcome_email_subject'];
    $body = $_POST['welcome_email_message'];
    $check = mysql_query("SELECT * FROM `mail_template` where `code` = 'welcome_email' ");
    if (mysql_num_rows($check) > 0) {
        mysql_query("update `mail_template` set `subject` = '$subject',`body` = '$body' where `code` = 'welcome_email' ");
    } else {
        mysql_query("insert into `mail_template` (`code`, `subject`, `body`) values ('welcome_email' ,'$subject', '$body') ");
    }
    
    //save-update referral_notification
    $subject = $_POST['referral_notification_email_subject'];
    $body = $_POST['referral_notification_email_message'];
    $check = mysql_query("SELECT * FROM `mail_template` where `code` = 'referral_notification' ");
    if (mysql_num_rows($check) > 0) {
        mysql_query("update `mail_template` set `subject` = '$subject',`body` = '$body' where `code` = 'referral_notification' ");
    } else {
        mysql_query("insert into `mail_template` (`code`, `subject`, `body`) values ('referral_notification' ,'$subject', '$body') ");
    }
    
    //save-update referral_notification
    $subject = $_POST['banner_ad_approval_email_subject'];
    $body = $_POST['banner_ad_approval_email_message'];
    $check = mysql_query("SELECT * FROM `mail_template` where `code` = 'banner_ad_approval' ");
    if (mysql_num_rows($check) > 0) {
        mysql_query("update `mail_template` set `subject` = '$subject',`body` = '$body' where `code` = 'banner_ad_approval' ");
    } else {
        mysql_query("insert into `mail_template` (`code`, `subject`, `body`) values ('banner_ad_approval' ,'$subject', '$body') ");
    }
    
    //save-update sponsor_ad_approval
    $subject = $_POST['sponsor_ad_approval_email_subject'];
    $body = $_POST['sponsor_ad_approval_email_message'];
    $check = mysql_query("SELECT * FROM `mail_template` where `code` = 'sponsor_ad_approval' ");
    if (mysql_num_rows($check) > 0) {
        mysql_query("update `mail_template` set `subject` = '$subject',`body` = '$body' where `code` = 'sponsor_ad_approval' ");
    } else {
        mysql_query("insert into `mail_template` (`code`, `subject`, `body`) values ('sponsor_ad_approval' ,'$subject', '$body') ");
    }
    
}

//get all options
$options = mysql_query("SELECT * FROM `mail_template`");
$allOprionsArray = array();
while ($option = mysql_fetch_array($options)) {
    $optionArray = array();
    $optionArray['body'] = $option['body'];
    $optionArray['subject'] = $option['subject'];
    $allOprionsArray[$option['code']] = $optionArray;
}
?>
<div id="page-wrapper">

    <div class="row">
        <div class="col-lg-12">
            <h1>Admin Settings <small>Update Emails</small></h1>
            <?php if (isset($_POST) && $_POST) : ?>
                <div class="alert alert-success alert-dismissable">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                    Settings updated successfully.
                </div>
            <?php endif; ?>

            <form class="form-horizontal" role="form" method="post">
                <p class="field-note">Change the email contents if you wants.</p>
                <p class="field-note">You can use the following tags for personalized mailing and these will automatically get replaced by user information.</p>
                <p class="field-note">{name} for Name of the member</p>
                <p class="field-note">{username} for username of the member</p>
                <p class="field-note">{password} for the password of the member</p>
                <p class="field-note">{email} for the Email Address of the member</p>
                <p class="field-note">{sitename} for your sitename like Viral Marketing</p>
                <p class="field-note">{siteurl} for your website url like http://www.google.com</p>

                <div class="form-group">
                    <label for="confirmation_email_subject" class="col-sm-2 control-label">Confirmation Email Subject</label>
                    <div class="col-sm-10">
                        <input type="text" value="<?php
                        if (isset($allOprionsArray['confirmation_email']['subject'])) {
                            echo $allOprionsArray['confirmation_email']['subject'];
                        }
                        ?>" class="form-control" name="confirmation_email_subject" id="confirmation_email_subject">
                    </div>
                </div>
                <div class="form-group">
                    <label for="confirmation_email_message" class="col-sm-2 control-label">Confirmation Email Message</label>
                    <div class="col-sm-10">
                        <textarea class="form-control" rows="10" name="confirmation_email_message" id="confirmation_email_message"><?php
                            if (isset($allOprionsArray['confirmation_email']['body'])) {
                                echo $allOprionsArray['confirmation_email']['body'];
                            }
                            ?></textarea>
                    </div>
                </div>

                <div class="form-group">
                    <label for="welcome_email_subject" class="col-sm-2 control-label">Welcome Email Subject</label>
                    <div class="col-sm-10">
                        <input type="text" value="<?php
                        if (isset($allOprionsArray['welcome_email']['subject'])) {
                            echo $allOprionsArray['welcome_email']['subject'];
                        }
                        ?>" class="form-control" name="welcome_email_subject" id="welcome_email_subject">
                    </div>
                </div>
                <div class="form-group">
                    <label for="welcome_email_message" class="col-sm-2 control-label">Welcome Email Message</label>
                    <div class="col-sm-10">
                        <textarea class="form-control" rows="10" name="welcome_email_message" id="welcome_email_message"><?php
                            if (isset($allOprionsArray['welcome_email']['body'])) {
                                echo $allOprionsArray['welcome_email']['body'];
                            }
                            ?></textarea>
                    </div>
                </div>

                <p class="field-note">Referral Notification Email to sponsor when downline member activates the free account:</p>
                <p class="field-note">You can use the following tags for showing the referred member details to sponsor.</p>

                <p class="field-note">{refname} for Referred Member Name</p>
                <p class="field-note">{refusername} for Referred Member Username</p>
                <p class="field-note">{refemail} for the Referred Member Email Address</p>

                <div class="form-group">
                    <label for="referral_notification_email_subject" class="col-sm-2 control-label">Referral Email Subject</label>
                    <div class="col-sm-10">
                        <input type="text" value="<?php
                        if (isset($allOprionsArray['referral_notification']['subject'])) {
                            echo $allOprionsArray['referral_notification']['subject'];
                        }
                        ?>" class="form-control" name="referral_notification_email_subject" id="referral_notification_email_subject">
                    </div>
                </div>
                <div class="form-group">
                    <label for="referral_notification_email_message" class="col-sm-2 control-label">Referral Email Message</label>
                    <div class="col-sm-10">
                        <textarea class="form-control" rows="10" name="referral_notification_email_message" id="referral_notification_email_message"><?php
                            if (isset($allOprionsArray['referral_notification']['body'])) {
                                echo $allOprionsArray['referral_notification']['body'];
                            }
                            ?></textarea>
                    </div>
                </div>
                
                <div class="form-group">
                    <label for="banner_ad_approval_email_subject" class="col-sm-2 control-label">Banner Ad Approval Email Subject</label>
                    <div class="col-sm-10">
                        <input type="text" value="<?php
                        if (isset($allOprionsArray['banner_ad_approval']['subject'])) {
                            echo $allOprionsArray['banner_ad_approval']['subject'];
                        }
                        ?>" class="form-control" name="banner_ad_approval_email_subject" id="banner_ad_approval_email_subject">
                    </div>
                </div>
                <div class="form-group">
                    <label for="banner_ad_approval_email_message" class="col-sm-2 control-label">Banner Ad Approval Email Message</label>
                    <div class="col-sm-10">
                        <textarea class="form-control" rows="10" name="banner_ad_approval_email_message" id="banner_ad_approval_email_message"><?php
                            if (isset($allOprionsArray['banner_ad_approval']['body'])) {
                                echo $allOprionsArray['banner_ad_approval']['body'];
                            }
                            ?></textarea>
                    </div>
                </div>

                <p class="field-note"><strong>Note:</strong> Please don't remove {id} and {password} tags from the above email otherwise advertiser won't be able to track the ad campaign.</p>
                <div class="form-group">
                    <label for="sponsor_ad_approval_email_subject" class="col-sm-2 control-label">Sponsor Ad Approval Email Subject</label>
                    <div class="col-sm-10">
                        <input type="text" value="<?php
                        if (isset($allOprionsArray['sponsor_ad_approval']['subject'])) {
                            echo $allOprionsArray['sponsor_ad_approval']['subject'];
                        }
                        ?>" class="form-control" name="sponsor_ad_approval_email_subject" id="sponsor_ad_approval_email_subject">
                    </div>
                </div>
                <div class="form-group">
                    <label for="sponsor_ad_approval_email_message" class="col-sm-2 control-label">Sponsor Ad Approval Email Message</label>
                    <div class="col-sm-10">
                        <textarea class="form-control" rows="10" name="sponsor_ad_approval_email_message" id="sponsor_ad_approval_email_message"><?php
                            if (isset($allOprionsArray['sponsor_ad_approval']['body'])) {
                                echo $allOprionsArray['sponsor_ad_approval']['body'];
                            }
                            ?></textarea>
                    </div>
                </div>
                
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-primary">Update</button>
                    </div>
                </div>
            </form>

        </div>
    </div><!-- /.row -->

</div><!-- /#page-wrapper -->
<?php include_once dirname(__FILE__) . '/footer.php'; ?>

